import { ParamArr} from './paramarr'

export default class CPushMessages {
    constructor(lpfError) {
        this.lpfError = lpfError;
        this.lstObject = new Array();
        this.NoResult = '{B86254DE-8CDA-497D-A185-76432D03409E}';
    }

   getMessagesStr = function (check) {
        var msgs = [];
        for (var i = this.lstObject.length - 1; i >= 0; i--) {
            var obj = this.lstObject[i];
            var cls = false;
            // 兼容IE11
            try {
                cls = obj.isClosed();
            } catch (err) {
                cls = true;
            }
            if ((check != false) && cls) {
                this.lstObject.splice(i, 1);
                continue;
            }
            for (var j = 0; j < obj.messages.length; j++) {
                var msg = obj.messages[j];
                if (msgs.indexOf(msg) == -1)
                    msgs.push(msg);
            }
        }
        msgs.sort();
        return msgs.join(",");
    }
    getMessages = function (vPar, sError) {
            // 错误处理
        if (sError != "" && sError !== this.NoResult && sError != '流程已经加入队列。') {
            if (window.top.mainMessages.lpfError != undefined) {
                window.top.mainMessages.lpfError(vPar, sError);
            }
            return;
        }
        // 缓存的消息
        if (vPar.getBool("Messages")) {
            for (var i = 0; i < vPar.getInt('Count'); i++) {
                var vTmp = new ParamArr(parseWebJson(vPar.getStr(i)));
                this.postMessageToObjects(vTmp);    // 广播消息包
            }
        } else {
            this.postMessageToObjects(vPar);        // 广播消息包
        }
    }

    /*
     * 注册消息
    */
    regMessages = function (obj, messages, isReg) {
        if (obj == undefined || messages == undefined || messages.length == 0)
            return;

        var msg1 = this.getMessagesStr();

        if (obj.messages == undefined)
            obj.messages = new Array();
        for (var i = 0; i < messages.length; i++) {
            var msg = messages[i];
            if (obj.messages.indexOf(msg) == -1)
                obj.messages.push(msg);
        }
        if (this.lstObject.indexOf(obj) == -1)
            this.lstObject.push(obj);

        // 比较消息
        var msg2 = this.getMessagesStr();
        if (msg1 == msg2)
            return;

        if (isReg == undefined || isReg) {
            this.regWebJsonMessage(msg2);
        }
    }
    postMessageToObjects = function (vPar, againReg) {
        var bReg = false;
        var msg1 = "";

        var msg = vPar.getMsgId();
        for (var i = this.lstObject.length - 1; i >= 0; i--) {
            var obj = this.lstObject[i];
            var cls = false;
            // 兼容IE11
            try {
                cls = obj.isClosed();
            } catch (err) {
                cls = true;
            }
            if (cls) {
                if (bReg == false) {
                    msg1 = this.getMessagesStr(false);
                }
                this.lstObject.splice(i, 1);
                bReg = true;
                continue;
            }
            if (obj.messages.indexOf(msg) != -1 && obj.WndProc != undefined) {
                //解决因消息推送到页面，任何一个错误导致整个消息推送系统瘫痪的问题
                try {
                    obj.WndProc(msg, vPar);
                } catch (e) {
                    //打印错误
                    console.error(e);
                }

            }
        }

        if (bReg && (againReg == undefined || againReg != false)) {
            var msg2 = this.getMessagesStr();
            if (msg1 != msg2) {
                this.regWebJsonMessage(msg2);
            }
        }
    }
    
    regWebJsonMessage = function(msg){
        if(!top.mainSocketObj || top.mainSocketObj.ws.readyState != 1) return;
        msg  = msg || this.getMessagesStr();
        var vPar = new ParamArr();
        vPar.setStr("Msgs", msg);
        top.mainSocketObj.callServerFuncSocket("THttpDM", "RegWebJsonMessage", vPar);
    }
}